package SwordOffer;

import java.util.ArrayList;
import java.util.List;

public class Sword_Offer_34 {

    private List<List<Integer>> rst;

    public List<List<Integer>> pathSum(TreeNode root, int target) {
        rst = new ArrayList<>();
        dps(root, target, new ArrayList<>());
        return rst;
    }

    private void dps(TreeNode node, int val, List<Integer> list) {
        if (node == null) {
            return;
        }
        val -= node.val;
        list.add(node.val);
        if (node.left == null && node.right == null && val == 0) {
            rst.add(new ArrayList<>(list));
        }
        dps(node.left, val, list);
        dps(node.right, val, list);
        list.remove(list.size() - 1);
    }

    static class TreeNode {

        int val;
        TreeNode left;
        TreeNode right;

        TreeNode() {
        }

        TreeNode(int val) {
            this.val = val;
        }

        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }
}
